package com.garlic.face.jlf.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;
import com.garlic.face.jlf.transaction.constances.JLFConstant;

/**
 * Created by shiyanqi on 18/9/10.
 */

public class WeightTipsDialog extends Dialog implements View.OnClickListener {

	private static volatile WeightTipsDialog weightTipsDialog = null;
	private WeightTipsDialog.CallBack callBack;
	private String type;
	private String weightMsg;
	private TextView weight_msg;
	private TextView weight_title;
	private TextView weight_content;
	private Context context;
	private ImageView tipImage;
	private String lastType = "";

	public WeightTipsDialog(@NonNull Context context, String type, String weightMsg) {
		super(context);
		this.context = context;
		this.type = type;
		this.weightMsg = weightMsg;
	}


	public void setCallBack(WeightTipsDialog.CallBack callBack) {
		this.callBack = callBack;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	private void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.popup_window_weight, null);
		setContentView(view);
		FrameLayout frameLayout = view.findViewById(R.id.popup_window_weight_layout);
		weight_title = view.findViewById(R.id.popup_window_weight_title);
		weight_content = view.findViewById(R.id.popup_window_weight_content);
		tipImage = view.findViewById(R.id.tip_image);
		setContent(type);
		weight_msg = view.findViewById(R.id.popup_window_weightmsg);
		weight_msg.setText(weightMsg);
		Button btn_help = view.findViewById(R.id.popup_window_weight_help);
		ImageView btn_admin = view.findViewById(R.id.popup_window_weight_admin);
		frameLayout.setOnClickListener(this);
		btn_help.setOnClickListener(this);
		btn_admin.setOnClickListener(this);
	}

	public void setContent(String type){
		switch (type){
			case JLFConstant.WEIGHT_START_VERIFY:
				weight_title.setText(R.string.weight_dialog_verify_title);
				weight_content.setText(R.string.weight_dialog_verify_content);
				tipImage.setImageResource(R.mipmap.prompt_icon);
				if(!type.equals(lastType)){
					App.playSound("e2");
					lastType = type;
				}
				break;
			case JLFConstant.WEIGHT_BAG_DLG:
				weight_title.setText(R.string.weight_dialog_bag_title);
				weight_content.setText(R.string.weight_dialog_bag_content);
				tipImage.setImageResource(R.mipmap.promptmessage_icon);
				if(!type.equals(lastType)){
					App.playSound("e1");
					lastType = type;
				}
				break;
			case JLFConstant.WEIGHT_UNEXCEPTED_DLG:
				weight_title.setText(R.string.weight_dialog_unexcepted_title);
				weight_content.setText(R.string.weight_dialog_unexcepted_content);
				tipImage.setImageResource(R.mipmap.prompt_icon);
				if(!type.equals(lastType)){
					App.playSound("e2");
					lastType = type;
				}
				break;
			case JLFConstant.WEIGHT_OVER_DLG:
				weight_title.setText(R.string.weight_dialog_over_title);
				weight_content.setText(R.string.weight_dialog_over_content);
				tipImage.setImageResource(R.mipmap.needhelp_icon);
				if(!type.equals(lastType)){
					App.playSound("e4");
					lastType = type;
				}
				break;
			case JLFConstant.WEIGHT_LOW_DLG:
				weight_title.setText(R.string.weight_dialog_low_title);
				weight_content.setText(R.string.weight_dialog_low_content);
				tipImage.setImageResource(R.mipmap.promptmessage_icon);
				if(!type.equals(lastType)){
					App.playSound("e1");
					lastType = type;
				}
				break;
			case JLFConstant.WEIGHT_CANCEL_LOW:
				weight_title.setText(R.string.weight_dialog_cancel_low_title);
				weight_content.setText(R.string.weight_dialog_cancel_low_content);
				tipImage.setImageResource(R.mipmap.prompt_icon);
				if(!type.equals(lastType)){
					App.playSound("e2");
					lastType = type;
				}
				break;
			case JLFConstant.WEIGHT_CANCEL_OVER:
				weight_title.setText(R.string.weight_dialog_cancel_over_title);
				weight_content.setText(R.string.weight_dialog_cancel_over_content);
				tipImage.setImageResource(R.mipmap.prompt_icon);
				if(!type.equals(lastType)){
					App.playSound("e7");
					lastType = type;
				}
				break;
			default:
				break;
		}
	}

//	public void setWeightMsg(String goodName){
//		if(goodName != null){
//			weight_msg.setText(goodName);
//		}
//	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.popup_window_weight_layout:
				break;
			case R.id.popup_window_weight_help:
				App.playSound("e4");
				callBack.helpClick(view);
				break;
			case R.id.popup_window_weight_admin:
				callBack.adminClick(view);
				break;
			default:
				break;
		}
	}

	public void stopSound() {
		lastType = "";
		App.getSoundMgr().stopPlay();
	}

	public interface CallBack {
		void adminClick(View view);

		void helpClick(View view);
	}


}
